/**
 * Created by yuweimei on 2018/4/29.
 */
app.controller('review', function($scope, $http) {
	$(".review_bottom input").keyup(function() {
		$(".send").css("background", "#31c27c");
	})
	$(".review_bottom input").blur(function() {
		$(".send").css("background", " rgba(0,0,0,0.4)");
	})

	if($(".review_bottom input").val() == "") {
		$(".send").css("background", " rgba(0,0,0,0.4)");
	}
	var token = localStorage.getItem('token');
	var tourdynaInfoId = sessionStorage.getItem("tourdynaInfoId");

	$scope.dynamicInfo = {};
	$scope.commentList = [];
	$scope.tourdynaInfo = function(page) {
		$scope.page = page ? page : 1;
		// 若该加载为初始化加载，即没有传入page参数
		if(!page) {
			$scope.page = 1;
		}

		$http({
			method: 'get',
			url: app.netPath + '/dynamic/info',
			headers: {
				'Authorization': token
			},
			params: {
				page: $scope.page,
				tourdynaId: tourdynaInfoId
			}
		}).then(function(res) {
			var infiniteScroll = $('.infinite-scroll');
			var infiniteScrollPreloader = $('.infinite-scroll-preloader');
			if(!res.data.error) {
				if(res.data.data.user.userAvatarAddress) {
					res.data.data.user.userAvatarAddress = app.netPath + '/resources/img/' + res.data.data.user.userAvatarAddress;
				}
				//显示评论动态发布者
				$scope.userNickName = res.data.data.user.userNickName;
				$scope.userId = res.data.data.user.userId;
				$scope.defautUserId = res.data.data.user.userId;
				$scope.parentId = 0;
				if(res.data.data.tourdynaImg) {
					//得到每条动态的图片数组
					var dynamicImgs = app.getDynamicImg(res.data.data.tourdynaImg);
					for(var j = 0; j < dynamicImgs.length; j++) {
						dynamicImgs[j] = app.netPath + '/resources/taskImg/' + dynamicImgs[j];
						app.buildImageUrl(dynamicImgs, j, function() {
							$scope.$digest();
						});
					}
					res.data.data.tourdynaImg = dynamicImgs;
				}
				var lat = res.data.data.positionX;
				var lng = res.data.data.positionY;
				res.data.data.distance = app.getGreatCircleDistance(lat, lng);
				if(res.data.data.distance >= 1000) {
					res.data.data.distance = (app.getGreatCircleDistance(lat, lng) / 1000).toFixed(2) + "千米";
				} else {
					res.data.data.distance = app.getGreatCircleDistance(lat, lng) + "米";
				}
				//无直接评论
				if(res.data.data.commentList == undefined) {
					// 加载完毕，则注销无限加载事件，以防不必要的加载
					$.detachInfiniteScroll(infiniteScroll);
					// 删除加载提示符
					infiniteScrollPreloader.hide();
				} else {

					for(var i = 0; i < res.data.data.commentList.length; i++) {
						if(res.data.data.commentList[i].user1.userAvatarAddress) {
							res.data.data.commentList[i].user1.userAvatarAddress = app.netPath + '/resources/img/' + res.data.data.commentList[i].user1.userAvatarAddress;
						}
						$scope.commentList.push(res.data.data.commentList[i]);
					}
					// 启用无限加载事件
					$.attachInfiniteScroll(infiniteScroll);
					$scope.thispage = res.data.data.commentList.length % 10 == 0 ? res.data.data.commentList.length / 10 : res.data.data.commentList.length / 10 + 1;
					$scope.thispage = parseInt($scope.thispage);
					console.log($scope.thispage);
					if(res.data.allPage <= $scope.thispage) {
						// 加载完毕，则注销无限加载事件，以防不必要的加载
						$.detachInfiniteScroll(infiniteScroll);
						// 删除加载提示符
						infiniteScrollPreloader.hide();

					}
				}
				$scope.dynamicInfo = res.data.data;
				console.log($scope.dynamicInfo);

				$scope.isOnloading = false;

			} else {
				// 加载完毕，则注销无限加载事件，以防不必要的加载
				$.detachInfiniteScroll(infiniteScroll);
				// 删除加载提示符
				infiniteScrollPreloader.hide();
			}
		});

	};
	//评论动态
	$scope.dynamicComment = "";
	$scope.userNickName = "";
	//动态发布者id
	$scope.defautUserId = null;
	$scope.userId = null;
	$scope.parentId = null;
	$scope.review = function() {
		if(!token) {
			//判断有无证书
			$.toast("亲，登录个彡");
			// location.href = '#login';
			return;
		} else {
			if($scope.dynamicComment != "") {
				$scope.comment = {
					"tourdynamicsId": tourdynaInfoId,
					'replayUserid': $scope.userId,
					'parentId': $scope.parentId,
					'commentContent': $scope.dynamicComment
				};
				console.log($scope.comment);
				$http({
					method: 'POST',
					url: app.netPath + '/comment/add',
					headers: {
						Authorization: token
					},
					data: $scope.comment
				}).then(function(res) {
					if(!res.data.error) {
						$.toast('评论成功！');
						$scope.dynamicComment = "";
						$scope.commentList = [];
						$scope.tourdynaInfo();
					} else {
						$.toast(res.data.msg)
					}
				})
			} else {
				$.toast("评论不能为空哟");
			}
		}
	};
	//选择评论对象
	$scope.updateReviewperson = function(userNickName, userId, commentId) {
		$scope.dynamicComment = "";
		$scope.userId = userId;
		$scope.userNickName = userNickName;
		if($scope.userId == $scope.defautUserId) {
			//直接评论
			$scope.parentId = 0;
		} else {
			//回复评论
			$scope.parentId = commentId;
		}
	};
	//跳转otherPerson详情页
	$scope.GoToOtherUser = function(userId) {
		sessionStorage.setItem('OtherUserId', userId);
	};
	//跳转回复详情页
	$scope.gotoReviewMore = function(commentId) {
		sessionStorage.setItem('commentInfoId', commentId);
	}
});